import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/home_business/special_topic_detail/specail_topics_detail_controller.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';
import 'package:tiktokadult/tools_base/widget/follow_button.dart';

import '../../../hj_model/home/module_detail_model.dart';
import '../home_cell_style/video_simple_cell_widget.dart';

class SpecailTopicsDetailPage extends StatelessWidget {
  final AllSection? originModel;

  const SpecailTopicsDetailPage(this.originModel, {super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SpecailTopicsController>(
      init: SpecailTopicsController(originModel),
      builder: (logic) {
        return Scaffold(
          body: NestedScrollView(
            controller: logic.scrollController,
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return [
                SliverAppBar(
                  expandedHeight: 44 + 154,
                  centerTitle: false,
                  titleSpacing: 0,
                  title: _buildAppBarTitle(logic),
                  leading: InkWell(
                    onTap: () => Get.back(),
                    child: Icon(
                      Icons.arrow_back_ios,
                      color: Colors.white,
                    ),
                  ),
                  backgroundColor: Colors.black,
                  pinned: true,
                  floating: false,
                  elevation: 0,
                  // excludeHeaderSemantics: true,
                  flexibleSpace: FlexibleSpaceBar(
                    collapseMode: CollapseMode.pin,
                    background: Container(
                      height: 44 + 154,
                      child: Stack(
                        fit: StackFit.expand,
                        children: [
                          Image.asset("section_header_bg.webp".homePath),
                          Container(
                            padding: EdgeInsets.fromLTRB(12, 44, 12, 0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                48.sizeBoxH,
                                Row(
                                  children: [
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            logic.title,
                                            style: textStyle(16, Color(0xE5FFFFFF), FontWeight.w500),
                                          ),
                                          4.sizeBoxH,
                                          Text(
                                            "作品数${logic.originModel?.workCount ?? 0}    粉丝数13.3w ",
                                            style: textStyle(12, Color(0xE5FFFFFF), FontWeight.w400),
                                          ),
                                          4.sizeBoxH,
                                          Text(
                                            logic.originModel?.desc ?? "",
                                            style: textStyle(12, Color(0xff989898), FontWeight.w400),
                                          ),
                                        ],
                                      ),
                                    ),
                                    10.sizeBoxW,
                                    Obx(
                                      () => FollowButtonSimple(
                                        logic.isFollow,
                                        isLoading: logic.isFollowLoading.value,
                                      ),
                                    ),
                                  ],
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  bottom: PreferredSize(
                    child: Container(
                      height: 44,
                      color: Colors.black,
                      child: Row(
                        children: [
                          12.sizeBoxW,
                          Expanded(child: TabBar(
                            tabAlignment: TabAlignment.fill,
                            tabs: logic.sortTitles.map((e) {
                              return Container(
                                alignment: Alignment.center,
                                child: Text(e),
                              );
                            }).toList(),
                            labelStyle: TextStyle(fontSize: 14),
                            labelColor: Color(0xE5FFFFFF),
                            unselectedLabelStyle: TextStyle(fontSize: 14),
                            unselectedLabelColor: Color(0x73FFFFFF),
                            controller: logic.tabController,
                            padding: EdgeInsets.zero,
                            isScrollable: false,
                            onTap: (index) {
                              logic.sort = index;
                              logic.fetchPageData(showLoading: true);
                            },
                            labelPadding: EdgeInsets.zero,
                            indicator: BoxDecoration(),
                          ),),
                          Container(
                            padding: EdgeInsets.only(right: 12),
                            child: InkWell(
                              onTap: logic.onTooleAction,
                              child: Row(
                                children: [
                                  Text(
                                    '切换',
                                    style: TextStyle(
                                      fontSize: 12,
                                      color: Colors.white.withOpacity(.6),
                                    ),
                                  ),
                                  SizedBox(width: 2),
                                  AnimatedSwitcher(
                                    duration: Duration(milliseconds: 300),
                                    transitionBuilder: (child, anim) {
                                      return ScaleTransition(child: child, scale: anim);
                                    },
                                    child: Image.asset(
                                      logic.isGridStyle.value ? 'grid_style.webp'.homePath : 'list_style.webp'.homePath,
                                      key: ValueKey(logic.isGridStyle.value),
                                      width: 18,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    preferredSize: Size.fromHeight(44),
                  ),
                ),
              ];
            },
            body: Column(
              children: [
                Expanded(
                  child: pullYsRefresh(
                    onInit: (ctr) => logic.refreshController = ctr,
                    onRefresh: (_) => logic.fetchPageData(),
                    onLoading: (_) => logic.fetchPageData(isRefresh: false),
                    child: () {
                      if (logic.isLoading) return LoadingCenterWidget();
                      if (logic.dataSource.isEmpty) return CErrorWidget();
                      return GridView.builder(
                        padding: EdgeInsets.symmetric(horizontal: 10),
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: logic.isGridStyle.value ? 2 : 1,
                          crossAxisSpacing: 7,
                          mainAxisSpacing: 12,
                          childAspectRatio: logic.isGridStyle.value ? 191 / 174 : 343/223,
                        ),
                        itemCount: logic.dataSource.length,
                        itemBuilder: (BuildContext context, int index) {
                          return SizedBox(
                            width: double.infinity,
                            height: double.infinity,
                            child: VideoSimpleCellWidget(
                              videoModel: logic.dataSource[index],
                              textLines: logic.isGridStyle.value ? 2 : 1,
                            ),
                          );
                        },
                      );
                    }(),
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildAppBarTitle(SpecailTopicsController logic) {
    return Obx(() {
      if (logic.isShowAppMenu.value) {
        return Container(
          child: Row(
            children: [
              Text(
                "${logic.title ?? ""}",
                style: textStyle(16, Color(0xE5FFFFFF), FontWeight.w500),
              ),
              Spacer(),
              Obx(
                () => FollowButtonSimple(
                  logic.isFollow,
                  isLoading: logic.isFollowLoading.value,
                ),
              ),
              12.sizeBoxW,
            ],
          ),
        );
      }
      return SizedBox();
    });
  }
}
